package ar.com.coonocer.joy.mdg.util.staticcontent;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ar.com.coonocer.joy.mdg.util.Generator;

public class StaticContentFinder {

	private static String IGNORE_PACKAGE = ".svn";

	private static Log logger = LogFactory.getLog(StaticContentFinder.class);

	static List<File> getStaticContent(Object umlObject, File templatesFolder) {
		//
		String name = Generator.getNameForObject(umlObject);
		//
		logger.debug("Looking for Static Content for: " + name);
		//
		List<File> list = new ArrayList<File>();
		//
		File classFolder = new File(templatesFolder, name);
		if (!classFolder.exists()) {
			return list;
		}
		//
		logger.debug("Looking for Static Content folder in: " + classFolder );
		//
		File staticFolder = new File(classFolder, Generator.STATIC_CONTENT_FOLDER);
		if (!staticFolder.exists()) {
			return list;
		}
		//
		logger.debug("Looking for Static Content in: " + staticFolder);
		//
		return getStaticContent2(umlObject, staticFolder);
	}

	private static List<File> getStaticContent2(Object umlObject, File staticFolder) {
		//
		List<File> list = new ArrayList<File>();
		//
		File[] files = staticFolder.listFiles();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					list.add(files[i]);
				} else if (files[i].isDirectory()) {
					if (!files[i].getName().equals(IGNORE_PACKAGE)) {
						list.add(files[i]);
						list.addAll(getStaticContent2(umlObject, files[i]));
					}
				}
			}
		}
		//
		return list;
	}

}
